﻿using FullApp1.Modules.ModuleName.ViewModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FullApp1.Modules.ModuleName.Views
{
    /// <summary>
    /// Interaction logic for ViewA.xaml
    /// </summary>
    public partial class ExistingPageView : UserControl
    {
        public ExistingPageView()
        {
            InitializeComponent();
            Loaded += (s, e) => {
                if (DataContext is ExistingPageViewModel vm &&
                    vm.VerticalOffset > 0)
                {
                    Dispatcher.BeginInvoke(() => {
                        MainScrollViewer.ScrollToVerticalOffset(vm.VerticalOffset);
                    });
                }
            };
        }

        private void MainScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            if (DataContext is ExistingPageViewModel vm)
            {
                vm.IsScrolling = e.VerticalChange != 0;
                // 同步滚动位置到ViewModel
                vm.VerticalOffset = e.VerticalOffset;

                // 更新滚动条位置（如果使用自定义滚动条）
                CustomScrollBar.Value = e.VerticalOffset;
            }
        }

        // 在代码后置文件中添加事件处理
        private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            var scrollViewer = sender as ScrollViewer;
            if (scrollViewer == null) return;
            
            if (e.Delta > 0)

                scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - 50);
            else
                scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + 50);

            e.Handled = true;
        }
        
    }
}
